package com.my.app.document.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.my.app.document.model.ApiInfo;
import com.my.app.document.service.DocumentService;
import com.my.framework.web.annotation.Query;
import com.my.framework.web.mvc.controller.BaseController;

import lombok.Data;

@RequestMapping("/app/document")
@Controller
public class DocumentController extends BaseController {

	@Autowired
	private DocumentService documentService;

	@RequestMapping("/index")
	public void index() {

	}

	@RequestMapping(value = "/detail/{index}", method = RequestMethod.GET)
	public ModelAndView detail(@PathVariable Integer index) {
		List<ApiInfo> apis = documentService.getApiInfo();
		ModelAndView view = new ModelAndView();
		view.addObject(apis.get(index));
		view.setViewName("/app/document/detail");
		return view;
	}

	@Query(name = "获得treeView所需菜单数据", path = "/treeView")
	public List<Map> treeView() {
		List list = new ArrayList();
		TreeView parent = new TreeView();
		parent.setNodes(new ArrayList());
		list.add(parent);

		List<ApiInfo> apis = documentService.getApiInfo();
		int id = 0;
		for (ApiInfo info : apis) {
			if (parent.text == null) {
				parent.text =info.getSimpleName();
			}
			if (!parent.text.equals(info.getSimpleName())) {
				parent = new TreeView();
				parent.text = info.getSimpleName();
				parent.nodes = new ArrayList();
				list.add(parent);
			}

			TreeView children = new TreeView();
			children.text = info.getMethodName();
			children.id = id + "";
			parent.nodes.add(children);
			id++;
		}
		return list;
	}

	@Data
	public class TreeView {
		private String id;
		private List<TreeView> nodes;
		private String text;
	}
}
